<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Batch\BatchCustomerDelete;
use App\Admin\Grid\NGrid;
use App\Admin\Tools\Customer\CustomerCreateTool;
use App\Admin\Tools\Customer\CustomerDeleteTool;
use App\Admin\Tools\Customer\CustomerEditTool;
use App\Admin\Tools\Customer\CustomerFollowUpTool;
use App\Admin\Tools\Customer\CustomerRemarkTool;
use App\Admin\Tools\Customer\CustomerScoreTool;
use App\Admin\Tools\Customer\CustomerShareTool;
use App\Models\BlacklistModel;
use App\Models\ContractedCustomerModel;
use App\Models\CustomerModel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Grid\Displayers\Actions;
use Encore\Admin\Grid\Tools;
use Illuminate\Support\Facades\Auth;

class CustomerController extends AdminController
{
    protected $title = '重点客户';

    protected function grid(): NGrid
    {
        $grid = new NGrid(new CustomerModel);
        $grid->model()->leftJoin('admin_users', 'admin_users.id', 'customer.operator_id')
            ->select([
                'admin_users.id as admin_user_id',
                'customer.*',
                'admin_users.name'
            ])->orderByDesc('customer.id');

        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('customer_wechat', '微信');
                $filter->like('customer_name', __('customer.customerName'));
                $filter->equal('customer_mobile', __('l.mobile'));
            });
            $filter->column(1 / 2, function ($filter) {
                $filter->like('extends', '回访备注');
                $filter->equal('score', '客户星级')->select(__('customer.scoreArr'));
            });
        });

        if (!ISADMIN) {
            if (in_array(Auth::user()->roles->first()->slug, ['boss', 'finance'])) $grid->model()->where('customer.main_id', MAINID);
            else $grid->model()->where('customer.operator_id', USERID);
        }
        if(Auth::user()->roles->first()->slug == 'Administrator') $grid->column('id', 'ID')->sortable();
        $grid->column('customer_name', __('customer.customerName'))->display(function () {
            $mobile = $this->customer_mobile;
            if (!is_null($mobile)) {
                $blacklist = BlacklistModel::query()->where('mobile', $this->customer_mobile)->first();
                $contract = ContractedCustomerModel::query()->where(function ($query) use ($mobile) {
                    return $query->orWhere('male_mobile', $mobile)
                        ->orWhere('female_mobile', $mobile);
                })->first();
                if (!is_null($blacklist) && !is_null($contract)) return $this->customer_name . '<br /><span class="label " style="background-color: #1a2226;color:white">黑</span> &nbsp;<span class="label label-info">签</span>';
                else if (!is_null($blacklist) && is_null($contract)) return $this->customer_name . '<br /><span class="label " style="background-color: #1a2226;color:white">黑</span>';
                else if (is_null($blacklist) && !is_null($contract)) return $this->customer_name . '<br /><span class="label label-info">签</span>';
                else return $this->customer_name;
            } else return $this->customer_name;
        });
        $grid->column('age', __('l.age'));
        $grid->column('customer_gender', __('l.gender'))->using(__('l.genderArr'));
        $grid->column('customer_address', __('customer.customerAddress'));
        $grid->column('customer_mobile', __('l.mobile'))->display(function () {
            return is_null($this->customer_mobile) ? '' : maskMobile($this->customer_mobile);
        });
        $grid->column('customer_wechat', __('customer.customerWechat'))->style('max-width:100px;word-break:break-all;')->display(function () {
            return is_null($this->customer_wechat) ? '' : maskMobile($this->customer_wechat);
        });
        $grid->column('remark', __('l.remark'))->style('max-width:150px;word-break:break-all;')->display(function () {
            if (mb_strlen($this->remark) < 16) return $this->remark;
            else return view('tooltip ', [
                'data' => $this->remark
            ]);

        });
        $grid->column('information_resources', __('customer.informationResources'))->using(__('customer.informationResourcesArr'));
        $grid->column('score', '客户星级')->display(function () {
            return view('customer.score', [
                'id' => $this->id,
                'score' => $this->score,
                'name' => $this->customer_name
            ]);
        });
        $grid->column('name', '回访人')->display(function () {
            if (is_null($this->forward_extends)) return '';
            return json_decode($this->forward_extends, true)[0];

        });

        $grid->actions(function (Actions $action) {
            $action->disableEdit();
            $action->disableView();
            $action->disableDelete();
            $action->append(new CustomerFollowUpTool(
                $action->row->id,
                $action->row->customer_mobile,
                $action->row->customer_wechat,
                $action->row->remark,
                $action->row->extends
            ));
            $action->append(new CustomerEditTool($action->row->id));
            if (!$action->row->is_forward) $action->append(new CustomerShareTool($action->row->id));
        });

        $grid->batchActions(function ($batch) {
            $batch->disableDelete();
        });
        $grid->disableBatchActions();
        $grid->tools(function (Tools $tools) {
            $tools->append(new CustomerCreateTool);
        });
        $grid->disableActions(false);
        $grid->disableCreateButton();
        return $grid;
    }
}
